package it4s.service.api.factory;

import it4s.service.api.exception.ServiceAPIExeption;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import facebook4j.FacebookFactory;
import facebook4j.conf.ConfigurationBuilder;

public class FacebookFactoryInstance extends Factory {

	FacebookInstance facebookInstance;

	@Override
	public FacebookInstance createServiceInstance(IServiceInstance ser) {
		ConfigurationBuilder cb = new ConfigurationBuilder();
		facebookInstance = new FacebookInstance();
		Properties prop = new Properties();
		String propFileName = "META-INF/facebook4j.properties";

		InputStream inputStream = getClass().getClassLoader()
				.getResourceAsStream(propFileName);

		try {
			prop.load(inputStream);
		} catch (IOException e) {
			
			throw new ServiceAPIExeption(new FileNotFoundException("Archivo '"
					+ propFileName + "' NO EXISTE"), "Error");
		}

		cb.setDebugEnabled(true).setOAuthAppId(prop.getProperty("oauth.appId"))
				.setOAuthAppSecret(prop.getProperty("oauth.appSecret"))
				.setOAuthAccessToken(prop.getProperty("oauth.accessToken"))
				.setOAuthPermissions(prop.getProperty("oauth.permissions"));
		FacebookFactory ff = new FacebookFactory(cb.build());
		facebookInstance.setFacebook(ff.getInstance());
		return facebookInstance;
	}

}
